﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManager.BusinessLogic.Abstract;
using HotelManager.DataAccess.Model;
using HotelManager.DataAccess;

namespace HotelManager.WebUI.Controllers
{
    public class HotelController : Controller
    {
        private IHotelService _hotelService;

        public HotelController(IHotelService hotelService)
        {
            _hotelService = hotelService;
        }

        // TODO: Just test, delete it.
        public ActionResult List()
        {
            return View(_hotelService.GetAllHotels());
        }

        public ViewResult Details(int id)
        {
            List<Option> options = ViewBag.Options;
            HotelInfosModel model = new HotelInfosModel();
            model = _hotelService.GetHotelDescription(id);
            model.RoomTypes = GetRoomTypes(id);
            model.VoteTime = GetVote(id);
            model.TotalPoint = GetPoint(id);
            model.RatePoint = GetRate(id);
            return View(model);
        }
        public List<RoomType> GetRoomTypes(int hotelId)
        {

            using (var context = new HotelDbEntities())
            {
                var roomTypes = context.RoomTypes.Where(x => x.HotelId == hotelId).ToList();

                return roomTypes;
            }

        }
        public int GetVote(int hotelId)
        {
            using (var context = new HotelDbEntities())
            {
                var vote = context.Hotels.Where(x => x.Id == hotelId).Select(x => x.RateCount).FirstOrDefault();
                if (vote != null)
                {
                    int voteTime = Convert.ToInt32(vote);
                    return voteTime;
                }
                else
                {
                    return 0;
                }
                
            }
        }

        public int GetPoint(int hotelId)
        {
            using (var context = new HotelDbEntities())
            {
                var point = context.Hotels.Where(x => x.Id == hotelId).Select(x => x.TotalPoints).FirstOrDefault();
                if (point != null)
                {
                    int point1 = Convert.ToInt32(point);
                    int voteTime = GetVote(hotelId);
                    if (voteTime != 0)
                    {
                        return (point1 / voteTime);
                    }
                    else { return 0; }
                }
                else { return 0; }
                
                
            }
            
        }
        public List<HotelRate> GetRate(int hotelId)
        {
            
            using (var context = new HotelDbEntities())
            {
                var rate = context.HotelRates.Where(x => x.HotelId == hotelId).ToList();
                
                return rate;
                
            }
        }


        
    }
}
